package com.dk.service.impl;

import com.dk.mapper.MenuMapper;
import com.dk.mapper.RoleMenuMapper;
import com.dk.pojo.Menu;
import com.dk.pojo.TPerson;
import com.dk.service.MuneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class MunuServiceImpl implements MuneService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Override
    //菜单
    public  List<Map<String, Object>> getTree(TPerson t) {
        List<Menu> menulist = menuMapper.selectByUserRoule(t.getPid());
        List<Map<String, Object>> menuTree = getMenuTree(menulist, 0);
        Boolean b = redisTemplate.boundHashOps("user_tree").hasKey("user_id" + t.getPid());
        if (!b && menulist != null){
            redisTemplate.boundHashOps("user_tree").put("user_id" + t.getPid(),menuTree);
            return menuTree;
        }
        List<Map<String, Object>> list = (List<Map<String, Object>>) redisTemplate.boundHashOps("user_tree").get("user_id" + t.getPid());
        return list;
    }

    //菜单递归拼接
    public List<Map<String,Object>> getMenuTree(List<Menu> menulist,Integer id){
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < menulist.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menulist.get(i);
            if (menu.getParentid().equals(id)){
                map = new HashMap();
                map.put("id",menu.getId());
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                map.put("nodes",getMenuTree(menulist,menu.getId()));
            }
            if (map != null) {
                list.add(map);
                List<Map<String,Object>> l = (List<Map<String, Object>>) map.get("nodes");
                if (l.size() <= 0){
                    map.remove("nodes");
                }
            }
        }

        return list;
    }

    //ztree

    @Override
    public List<Map<String, Object>> getZtree() {
        List<Menu> menulist = menuMapper.selectByExample(null);
        List<Map<String, Object>> menuZtree = getMenuZtree(menulist, 0);
        return menuZtree;
    }

    //菜单递归拼接ztree
    public List<Map<String,Object>> getMenuZtree(List<Menu> menulist,Integer id){
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < menulist.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menulist.get(i);
            if (menu.getParentid().equals(id)){
                map = new HashMap();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                map.put("children",getMenuZtree(menulist,menu.getId()));
            }
            if (map != null) {
                list.add(map);
                List<Map<String,Object>> l = (List<Map<String, Object>>) map.get("children");
                if (l.size() <= 0){
                    map.remove("children");
                }
            }
        }

        return list;
    }

    //增加菜单
    @Override
    public Boolean insertTPerson(Menu menu) {
        try {
            menuMapper.insertSelective(menu);
            //同步redis（删除redis）
            redisTemplate.delete("user_tree");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //查询当前菜单对象
    @Override
    public Menu selectById(Integer pid) {
        Menu menu = menuMapper.selectByPrimaryKey(pid);
        return menu;
    }

    //修改菜单
    @Override
    public Boolean updatemEenu(Menu menu) {
        try {
            menuMapper.updateByPrimaryKeySelective(menu);
            //同步redis （删除）
            redisTemplate.delete("user_tree");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //删除菜单
    @Override
    public Boolean deleteTree(Integer id) {
        try {
            menuMapper.deleteByPrimaryKey(id);
            //同步redis （删除）
            redisTemplate.delete("user_tree");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    //拼接角色回显ztree
    @Override
    public List<Map<String, Object>> getzTreeChecked(Integer rid) {
        List<Menu> menulist = menuMapper.selectByExample(null);
        //查询当前角色对应的权限
        List<Integer> mids = roleMenuMapper.selectByRid(rid);
        List<Map<String, Object>> menuZtreeChecked = getMenuZtreeChecked(menulist, 0,mids);
        return menuZtreeChecked;
    }

    //回显菜单递归拼接ztree
    public List<Map<String,Object>> getMenuZtreeChecked(List<Menu> menulist,Integer id,List<Integer> mids){
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < menulist.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = menulist.get(i);
            if (menu.getParentid().equals(id)){
                map = new HashMap();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                if (mids !=null && mids.size() > 0 && mids.contains(menu.getId())){
                    map.put("checked",true);
                }
                map.put("children",getMenuZtreeChecked(menulist,menu.getId(), mids));
            }
            if (map != null) {
                list.add(map);
                List<Map<String,Object>> l = (List<Map<String, Object>>) map.get("children");
                if (l.size() <= 0){
                    map.remove("children");
                }
            }
        }

        return list;
    }
}



